package com.caimiao.miao.page.master.system.server;

import android.content.Context;
import android.text.TextUtils;

import com.caimiao.miao.R;
import com.caimiao.miao.http.rxjava.observer.CustomObserver;
import com.caimiao.miao.interfaces.BasePresenterImpl;
import com.caimiao.miao.library.utils.MatchUtil;
import com.caimiao.miao.library.utils.tip.ToastUtil;

public class ServerPresenter extends BasePresenterImpl<IServerContact.View, IServerContact.Model> implements IServerContact.Presenter {
    public ServerPresenter(IServerContact.View mView) {
        super(mView);
    }

    @Override
    protected IServerContact.Model getModel() {
        return new ServerModel();
    }

    @Override
    public void start(Context context) {
        mView.setUrl(mModel.getBaseUrl());
    }

    @Override
    public void save(String url) {
        if (TextUtils.isEmpty(url)) {
            ToastUtil.show(R.string.server_edit_hint);
            return;
        } else if (!MatchUtil.isCompleteUrl(url)) {
            ToastUtil.show(R.string.toast_server_match);
            return;
        }
        mModel.save(url)
                .subscribe(new CustomObserver<Boolean>(getCompositeDisposable()) {
                    @Override
                    public void onNext(Boolean aBoolean) {
                        ToastUtil.show(R.string.toast_server_success);
                        mView.close(null);
                    }
                });
    }
}
